package com.servlet;

import com.entity.Type;
import com.service.TypeService;
import com.service.impl.TypeServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/Type.let")
public class TypeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        TypeService typeService = new TypeServiceImpl();
        //1.设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //2.获取各种对象
        PrintWriter out = resp.getWriter();
        ServletContext application = req.getServletContext();
        String type = req.getParameter("type");
        switch (type){
            case "modify":
                int typeId =Integer.parseInt(req.getParameter("typeId"));
                String typeName = req.getParameter("typeName");
                int count = typeService.modify(typeId, typeName);
                if(count>0){
                    List<Type> types = typeService.getAll();
                    application.setAttribute("types",types);
                    out.println("<script>alert('修改成功');location.href= '/bgFoodKa/detail/cuisineList.jsp';</script>");
                }else {
                    out.println("<script>alert('修改失败');location.href= '/bgFoodKa/detail/updateCuisineList.jsp';</script>");
                }
                break;
                case "modifypre":
                    modifypre(req,resp,out,application);
                    break;
                case "add":
                    add(req,resp,out,application);
                    break;
                case "delete":
                    delete(req,resp,out,application);
                    break;
                case "query":
                    typeName=req.getParameter("typeName");
                    List<Type> typesGetByName=typeService.getByName(typeName);
                    if (typesGetByName!=null){
                        application.setAttribute("types",typesGetByName);
                        req.getRequestDispatcher("/bgFoodKa/detail/cuisineList.jsp").forward(req,resp);
                    }
                    break;

        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp, PrintWriter out, ServletContext application) {
        TypeService typeService = new TypeServiceImpl();
        int id = Integer.parseInt(req.getParameter("id"));
        int count = typeService.delete(id);
        if (count > 0) {
            List<Type> types = typeService.getAll();
            application.setAttribute("types", types);
            out.println("<script>alert('删除成功');location.href= '/bgFoodKa/detail/cuisineList.jsp';</script>");
        } else {
            out.println("<script>alert('删除失败,有子信息');location.href= '/bgFoodKa/detail/cuisineList.jsp';</script>");
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp, PrintWriter out, ServletContext application) {
        TypeService typeService = new TypeServiceImpl();
        String typeName = req.getParameter("typeName");
        int count = typeService.add(typeName);
        if(count>0){
            List<Type> types = typeService.getAll();
            //更新
            application.setAttribute("types",types);
            out.println("<script>alert('添加成功');location.href='/bgFoodKa/detail/cuisineList.jsp';</script>");
        }else {
            out.println("<script>alert('添加失败');location.href='/bgFoodKa/detail/updateCuisineList.jsp';</script>");
        }

    }

    private void modifypre(HttpServletRequest req, HttpServletResponse resp, PrintWriter out, ServletContext application) throws ServletException, IOException {
        TypeService typeService = new TypeServiceImpl();
        long id = Long.parseLong(req.getParameter("id"));
        Type foodType = typeService.getById(id);
        List<Type> FT=new ArrayList<>();
        FT.add(foodType);
        System.out.println(foodType);
        application.setAttribute("foodType",FT);
        req.getRequestDispatcher("/bgFoodKa/detail/updateCuisine.jsp").forward(req,resp);
    }
}
